package com.manage.controller;

import com.beans.Customer;
import com.beans.CustomerFavorite;
import com.beans.FollowRecord;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.manage.util.UserUtils;
import com.services.CustomerFavoriteService;
import com.services.CustomerService;
import com.services.FollowRecordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/22
 */
@Controller
@RequestMapping("customer")
public class CustomerController extends BaseController{

    @Autowired
    private CustomerService customerService;
    @Autowired
    private CustomerFavoriteService customerFavoriteService;
    @Autowired
    private FollowRecordService followRecordService;

    /**
     * 查询列表
     *
     * @param customer
     * @return
     */
    @RequiresPermissions("customer:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(Customer customer, Long relationId, HttpServletRequest request) {

        customer.setCreateBy(UserUtils.getUser().getUserId());
        request.setAttribute("customer", customer);
        return "customer/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param customer
     * @return
     */
    @RequiresPermissions("customer:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, Customer customer) {
        customer.setCreateBy(UserUtils.getUser().getUserId());
        return customerService.queryPageByCustomer(pageList, customer);
    }

    /**
     * 新增修改
     *
     * @param customer
     * @return
     */
    @RequiresPermissions("customer:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(Customer customer, Model model){
        FollowRecord followRecord = new FollowRecord();
        List followRecords = new ArrayList();

        if(customer.getCustomerId() != null){
            customer = customerService.queryById(customer.getCustomerId());

            followRecord.setRelationId(customer.getCustomerId());
            followRecord.setRelationType(Constants.twoLong);

            followRecords = followRecordService.queryList(followRecord);
        }

        model.addAttribute("customer", customer);
        model.addAttribute("followRecords", followRecords);


        return "customer/edit";
    }

    /**
     * 详情
     *
     * @param customer
     * @return
     */
    @RequiresPermissions("customer:detail")
    @RequestMapping("detail")
    public String detail(Customer customer, Model model){
        FollowRecord followRecord = new FollowRecord();
        List followRecords = new ArrayList();

        if(customer.getCustomerId() != null){
            customer = customerService.queryById(customer.getCustomerId());

            followRecord.setRelationId(customer.getCustomerId());
            followRecord.setRelationType(Constants.twoLong);

            followRecords = followRecordService.queryList(followRecord);
        }

        model.addAttribute("customer", customer);
        model.addAttribute("followRecords", followRecords);


        return "customer/detail";
    }

    @RequiresPermissions("customer:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Customer customer, HttpServletRequest request){
        int result = -1;
        customer.setCreateBy(UserUtils.getUser().getUserId());
        customer.setUpdateBy(UserUtils.getUser().getUserId());
        if (customer.getCustomerId() == null) {
            result = customerService.add(customer);
        } else {
            result = customerService.update(customer);
        }

        return "redirect:list";
    }

    @RequiresPermissions("customer:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long customerId) {
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        customer.setUpdateBy(UserUtils.getUser().getUserId());

        int result = customerService.delete(customer);

        //TODO  成功失败结果页面
        return result;
    }


    /***
     * 收藏
     * @param customerId
     */
    @RequiresPermissions("customer:collection")
    @RequestMapping("collection")
    @ResponseBody
    public Object collection(Long customerId) {
        int result = 0;
        CustomerFavorite customerFavorite = new CustomerFavorite();

        customerFavorite.setCustomerId(customerId);
        customerFavorite.setCreateBy(UserUtils.getUser().getUserId());
        customerFavorite.setUpdateBy(UserUtils.getUser().getUserId());
        customerFavorite.setStaus(Constants.zero);

        List houseFavorites = customerFavoriteService.queryList(customerFavorite);

        if(houseFavorites.size() == 0){
            result = customerFavoriteService.add(customerFavorite);
        }

        //TODO  成功失败结果页面
        return result;
    }

    /***
     * 取消收藏
     * @param customerId
     */
    @RequiresPermissions("customer:uncollection")
    @RequestMapping("uncollection")
    @ResponseBody
    public Object uncollection(Long customerId) {
        int result = 0;
        CustomerFavorite customerFavorite = new CustomerFavorite();

        customerFavorite.setCustomerId(customerId);
        customerFavorite.setCreateBy(UserUtils.getUser().getUserId());
        customerFavorite.setUpdateBy(UserUtils.getUser().getUserId());
        customerFavorite.setStaus(Constants.one);

        result = customerFavoriteService.delete(customerFavorite);

        //TODO  成功失败结果页面
        return result;
    }
}
